package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.service.UserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.JsonUtils;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {


    ResultVO resultVO=null;
    private UserService userService;

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取登录请求的三个参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");



        //获取session
        HttpSession session = req.getSession();
        String verCode = (String) session.getAttribute("verCode");

        userService = new UserServiceImpl();
        User user = userService.login(username, password);

        //比较用户输入的验证码和生成的验证码
        if (verCode.equalsIgnoreCase(imageCode)){
            //判断是否登录成功
            if (user ==null){
                resultVO=new ResultVO(200,"账号或者密码输入错误",null);
            }else {
                //登录成功之后将账号密码存入session

                resultVO=new ResultVO(200,"登录成功", user);
                System.out.println(resultVO);
                session.setAttribute("user",resultVO);

            }

        }else {
            //验证码不对
            resultVO=new ResultVO(1,"验证码有误",null);
        }

        System.out.println(resultVO);

        JsonUtils.toPrint(resultVO,resp);

    }


    public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        JsonUtils.toPrint(resultVO,resp);

    }

    //注销用户登录状态

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
//        session.invalidate();       //整个session对象失效
        session.removeAttribute("user");    //单个移除
        resultVO=new ResultVO(200,"注销成功",null);

        JsonUtils.toPrint(resultVO,resp);

    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取表单的数据 相当于getp...-->用工具获取 太多了 8个  beanutils工具
        User user = new User();
        Map<String, String[]> parameterMap = req.getParameterMap();
        //beanutils要求 对象中的属性和参数名要一一对应，类型需要兼容
        BeanUtils.populate(user,parameterMap);
        System.out.println(user);

        //操作数据库
        UserService userService = new UserServiceImpl();
        boolean flag=userService.add(user);
        if (flag){
            resultVO=new ResultVO(200,"添加成功",null);
        }else {
            resultVO=new ResultVO(500,"添加失败",null);
        }
        JsonUtils.toPrint(resultVO,resp);

    }


    //分页查询的方法
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");

        PageUtils<User> pageUtils =userService.findByPage(currentPage,pageSize,search);

        JsonUtils.toPrint(pageUtils,resp);

    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        //使用beanUtils封装请求参数到实体类中 前提要有实体类对象
        User user = new User();
        BeanUtils.populate(user,req.getParameterMap());
        Boolean b=userService.update(user);
        if (b){
            resultVO=new ResultVO(200,"修改成功",null);
        }else {
            resultVO=new ResultVO(500,"修改失败",null);
        }


        JsonUtils.toPrint(resultVO,resp);

    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        //删除多个，要用getParameterValues,返回的是一个数组
        //localhost/user?method=del&uid=1&uid=2&uid=3
        String[] uids = req.getParameterValues("uid");
        //System.out.println(Arrays.toString(uids));
        Boolean b=userService.batchDel(uids);
        if (b){
            resultVO=new ResultVO(200,"删除成功",null);
        }else {
            resultVO=new ResultVO(500,"删除失败",null);
        }

        JsonUtils.toPrint(resultVO,resp);

    }
}
